#include <iostream>
#include <string>
#include <cerrno>
#include <cstring>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>
#include <cassert>


#define Mode 0666
#define Path "./fifo"
using namespace std;
class fifo
{
public:
    //构造
    fifo(const string &path=Path)
        :_path(path)
    {
        umask(0);
        int n = mkfifo(path.c_str(),Mode);
        if(n==0)
        {
            cout<<"管道创建成功"<<endl;
        }
        else
        {
            cerr<<"管道创建失败,错误码:"<<errno<<"报错:"<<strerror(errno)<<endl;
        }

    }
    //析构
    ~fifo()
    {
        int n = unlink(_path.c_str());
        if(n==0)
        {
            cout<<"删除管道文件成功"<<endl;
        }
        else
        {
            cerr<<"管道删除失败,错误码:"<<errno<<"报错："<<strerror(errno)<<endl;
        }
    }
private:
    string _path;
};

class Sync
{
public:
    Sync()
        :wfd(-1)
        ,rfd(-1)
    {}
    void OpenRead()
    {
        rfd = open(Path,O_RDONLY);
        if(rfd<0)
        {
            exit(1);
        }
    }
    void OpenWrite()
    {
        wfd = open(Path,O_WRONLY);
        if(wfd<0)
        {
            exit(1);
        }
    }


    bool Wait()
    {
        bool ret = true;
        uint32_t c = 0;
        ssize_t n = read(rfd,&c,sizeof(uint32_t));
        if(n == sizeof(uint32_t))
        {
            cout<<"唤醒服务端，正在从共享内存读取中..."<<endl;
        }
        else if(n == 0)
        {
            ret = false;
        }
        else
        {
            return false;
        }
        return ret;
    }

    void Wakeup()
    {
        uint32_t c = 0;
        ssize_t n = write(wfd,&c,sizeof(c));
        assert(n==sizeof(uint32_t));
        cout<<"正在唤醒服务端..."<<endl;

    }

    ~Sync()
    {}

private:
    int wfd;
    int rfd;
};